using System;
using System.Collections.Generic;
using System.IO;
using Common;

namespace SExperiment
{
    public class BadPoint
    {
        public int WellNo { get; set; }

        public int ChannelNo { get; set; }

        public int[] RawPoints { get; private set; }

        public int[] RawMeltPoints { get; private set; }

        public BadPoint()
        {
            Clear();
        }

        private bool IsRawPointsEmpty()
        {
            int[] rawPoints = RawPoints;
            foreach (int num in rawPoints)
            {
                if (num > -1)
                {
                    return false;
                }
            }
            return true;
        }

        private bool IsRawMeltPointsEmpty()
        {
            int[] rawMeltPoints = RawMeltPoints;
            foreach (int num in rawMeltPoints)
            {
                if (num > -1)
                {
                    return false;
                }
            }
            return true;
        }

        public bool IsEmpty()
        {
            if (IsRawPointsEmpty())
            {
                return IsRawMeltPointsEmpty();
            }
            return false;
        }

        public void Clear()
        {
            RawPoints = new int[3] { -1, -1, -1 };
            RawMeltPoints = new int[3] { -1, -1, -1 };
        }

        public BadPoint Clone()
        {
            BadPoint badPoint = new BadPoint();
            badPoint.WellNo = WellNo;
            badPoint.ChannelNo = ChannelNo;
            for (int i = 0; i < 3; i++)
            {
                badPoint.RawPoints[i] = RawPoints[i];
                badPoint.RawMeltPoints[i] = RawMeltPoints[i];
            }
            return badPoint;
        }

        public void SaveToStream(StreamEx stream)
        {
            stream.Write(PropertyKey.Key_BadPoint);
            List<PropertyKey> list = new List<PropertyKey>();
            List<byte[]> list2 = new List<byte[]>();
            list.Add(PropertyKey.Key_WellNo);
            list2.Add(BytesConverter.GetBytes(WellNo));
            list.Add(PropertyKey.Key_ChannelNo);
            list2.Add(BytesConverter.GetBytes(ChannelNo));
            List<byte> list3 = new List<byte>();
            if (!IsRawPointsEmpty())
            {
                list.Add(PropertyKey.Key_RawPoints);
                list3.AddRange(BytesConverter.GetBytes(RawPoints.Length));
                for (int i = 0; i < RawPoints.Length; i++)
                {
                    list3.AddRange(BytesConverter.GetBytes(RawPoints[i]));
                }
                list2.Add(list3.ToArray());
            }
            list3.Clear();
            if (!IsRawMeltPointsEmpty())
            {
                list.Add(PropertyKey.Key_RawMeltPoints);
                list3.AddRange(BytesConverter.GetBytes(RawMeltPoints.Length));
                for (int j = 0; j < RawMeltPoints.Length; j++)
                {
                    list3.AddRange(BytesConverter.GetBytes(RawMeltPoints[j]));
                }
            }
            stream.Write(PropertyKey.Key_Index);
            stream.WriteIndex(list, list2);
            stream.Flush();
        }

        public bool ReadFromStream(StreamEx stream)
        {
            PropertyKey propertyKey = stream.ReadKey();
            if (propertyKey == PropertyKey.Key_BadPoint)
            {
                propertyKey = stream.ReadKey();
                if (propertyKey == PropertyKey.Key_Index)
                {
                    List<PropertyKey> keys;
                    List<byte[]> dates;
                    stream.ReadIndex(out keys, out dates);
                    for (int i = 0; i < keys.Count; i++)
                    {
                        switch (keys[i])
                        {
                            case PropertyKey.Key_WellNo:
                                WellNo = BytesConverter.BytesToInt(dates[i]);
                                break;
                            case PropertyKey.Key_ChannelNo:
                                ChannelNo = BytesConverter.BytesToInt(dates[i]);
                                break;
                            case PropertyKey.Key_RawPoints:
                                {
                                    StreamEx streamEx = new StreamEx();
                                    streamEx.Write(dates[i]);
                                    streamEx.Seek(0L, SeekOrigin.Begin);
                                    int val = streamEx.ReadInt();
                                    for (int k = 0; k < Math.Min(RawPoints.Length, val); k++)
                                    {
                                        RawPoints[k] = streamEx.ReadInt();
                                    }
                                    break;
                                }
                            case PropertyKey.Key_RawMeltPoints:
                                {
                                    StreamEx streamEx = new StreamEx();
                                    streamEx.Write(dates[i]);
                                    streamEx.Seek(0L, SeekOrigin.Begin);
                                    int val = streamEx.ReadInt();
                                    for (int j = 0; j < Math.Min(RawMeltPoints.Length, val); j++)
                                    {
                                        RawMeltPoints[j] = streamEx.ReadInt();
                                    }
                                    break;
                                }
                        }
                    }
                    return true;
                }
                stream.Seek(-4L, SeekOrigin.Current);
                return false;
            }
            stream.Seek(-4L, SeekOrigin.Current);
            return false;
        }
    }
}
